package com.flyee.io{
import com.flyee.utils.MyLog;

import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.messaging.channels.StreamingAMFChannel;
	
	
	public class Config{
		
		private static var config : Config;
		
		public var STATIC_SERVER : String;		
		public var FLEX_SERVER : String;	
		public var SOCKET_SERVER : String;	
		public var REMOTEOBJECT_AMF:String = "/messagebroker/amf";
		public var STREAMING_AMF:String = "/messagebroker/streamingamf";
		public var SOCKET_PORT : int;	
		
		private var _amfChannelSet:ChannelSet = null;
		private var _streamChannelSet:ChannelSet = null;
		
		private var callbackFn : Function;

		public function Config(singleton : Singleton, callbackFn : Function = null,configURL:String = null){
			var url:String= null;
			if(configURL)
			{
				url = configURL;
			}
			else
			{
//				url = "http://219.153.65.42/afteryuan_engine/resource/config.xml";
				url = "resource/config.xml";
			}
			var loadit:URLLoader = new URLLoader();
			loadit.addEventListener(Event.COMPLETE, completeHandler);
			this.callbackFn = callbackFn;
			loadit.load(new URLRequest(url));
		}
		
		public static function getInstance(callbackFn : Function = null,configURL:String = null) : Config {
			if(config==null)
				config = new Config(new Singleton(), callbackFn,configURL);
			return config;
		} 
	
		private function completeHandler(event:Event):void {
			var configXml:XML = new XML(event.currentTarget.data as String);
			STATIC_SERVER = configXml.staticServer;
			FLEX_SERVER = configXml.flexServer+"/flyee/flex.action";
			REMOTEOBJECT_AMF = configXml.flexServer + REMOTEOBJECT_AMF;
			STREAMING_AMF = configXml.flexServer + STREAMING_AMF;
			SOCKET_SERVER = configXml.socket;
			SOCKET_PORT = parseInt(configXml.port.toString());
            MyLog.info("Config is loaded");
			callbackFn.call(this);
		}
		
		public function getResourcePath() : String {
//			return "http://219.153.65.42/afteryuan_engine/";
			return "";
		}
		
		/**得到一个数据AMF普通通道*/
		public function get amfChannelSet():ChannelSet
		{
			if (_amfChannelSet == null)
			{
				var amfChannel:AMFChannel=new AMFChannel("channel", REMOTEOBJECT_AMF);
				_amfChannelSet=new ChannelSet();
				_amfChannelSet.channels=[amfChannel];
			}
			return _amfChannelSet;
		}

		/**得到一个数据AMF普通通道*/
		public function get streamChannelSet():ChannelSet
		{
			if (_streamChannelSet == null)
			{
				var amfChannel:StreamingAMFChannel=new StreamingAMFChannel("my-streaming-amf", STREAMING_AMF);
				_streamChannelSet=new ChannelSet();
				_streamChannelSet.channels=[amfChannel];
			}
			return _streamChannelSet;
		}

	}
	
}

class Singleton{}